import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ThemeManager {
	
	public static final int SMALL_STYLE = 0;
	public static final int BIG_STYLE = 1;
	
	public static final int CHAR_SPACING = 2;
	public static final int SPACE_WIDTH = 5;
	
	private static final String FILE1 = "/font/fontSS.png";
	private static final String FILE2 = "/font/fcg10.png";
	private static final String FILE3 = "/font/fcg14.png";
	
	private static final int H1 = 14;
	private static final int H2 = 14;
	private static final int H3 = 20;
	
	private static final char[] CHARS1 = {
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'.', ',', ':', '!', '?', '(', ')', '-', '\'', '/',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
		'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	};
	
	private static final char[] CHARS2 = {
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'.', ',', ':', '!', '?', '(', ')', '-', '\'', '/',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
		'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	};
	
	private static final char[] CHARS3 = {
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'.', ',', ':', '!', '?', '(', ')', '-', '\'', '/',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
		'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	};
	
	public static FontImage Small;
	public static FontImage Big;
	
	public static Image Balls;
	public static Image Digits;
	
	public static void detect(int screenWidth) {
		try {
			Digits = Image.createImage("/num.png");
			if (screenWidth < 234) {
				Small = new FontImage(FILE1, CHARS1, H1);
				Big = new FontImage(FILE2, CHARS2, H2);
				Balls = Image.createImage(screenWidth < 171 ?
						"/128x160/ball.png" : "/176x220/ball.png");
			} else {
				Small = new FontImage(FILE2, CHARS2, H2);
				Big = new FontImage(FILE3, CHARS3, H3);
				Balls = Image.createImage("/ball.png");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void drawString(Graphics g, String text, int x, int y,
			int style, int anchor) {
		Image[] imgs = getListImages(text, style);
		drawListImages(g, x, y, imgs, anchor);
	}
	
	public static void drawStringCenter(Graphics g, String text,
			int canvasWidth, int y, int style, int anchor) {
		Image[] imgs = getListImages(text, style);
		int sumWidth = 0;
		for (int i = 0; i < imgs.length; i++) {
			sumWidth += imgs[i] == null ? SPACE_WIDTH :
					(imgs[i].getWidth() + CHAR_SPACING);
		}
		int x = (canvasWidth - sumWidth) / 2;
		drawListImages(g, x, y, imgs, anchor);
	}

	private static void drawListImages(Graphics g, int x, int y,
			Image[] imgs, int anchor) {
		for (int i = 0; i < imgs.length; i++) {
			if (imgs[i] != null) {
				g.drawImage(imgs[i], x, y, Graphics.LEFT | anchor);
				x += imgs[i].getWidth() + CHAR_SPACING;
			} else {
				x += SPACE_WIDTH;
			}
		}
	}
	
	private static Image[] getListImages(String text, int style) {
		FontImage fontImage;
		switch (style) {
		case ThemeManager.SMALL_STYLE:
			fontImage = Small;
			break;
		default:
			fontImage = Big;
		}
		int len = text.length();
		Image[] imgs = new Image[len];
		for (int i = 0; i < len; i++) {
			imgs[i] = fontImage.cropImage(text.charAt(i));
		}
		return imgs;
	}
	
}
